package com.ericsson.common.date;

//import com.softtone.smp.bill.*;

/**
 * <p>Title: ����������ʱ��������</p>
 * <p>Description: ʵ�ֶ�ʱ��Ĳ���</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: SoftTone Corporation.</p>
 * @author Elaine Sun
 * @version 1.0
 */
public class HandleDate
{
	/**
	 * ���iС��10��ת��Ϊ��0��}λString
	 * @param i ��Ҫת��������
	 * @return ת������ַ�
	 */
	public static String toZeroStr(int i)
	{
		return (i < 10 ? "0" : "") + String.valueOf(i);
	}

	/**
	 * �õ�ָ�����ִ�����ȷ�·ݣ�С��1Ϊ1������12Ϊ12
	 * @param month �·�
	 * @return ��ȷ���·�
	 */
	public static int getCurMonth(int month)
	{
		return (month < 1) ? 1 : ((month > 12) ? 12 : month);
	}

	/**
	 * �õ�ָ�������յ���ȷ������
	 * @param year ��
	 * @param month ��
	 * @param day ��
	 * @return ��ȷ����
	 */
	public static int getCurDay(int year, int month, int day)
	{
		int maxday = getMaxDayOfMonth(year, month);
		return (day < 1) ? 1 : ((day > maxday) ? maxday : day);
	}

	/**
	 * �õ�ָ�����µ��������
	 * @param year ��
	 * @param month ��
	 * @return �������
	 */
	public static int getMaxDayOfMonth(int year, int month)
	{
		if (month == 4 || month == 6 || month == 9 || month == 11)
		{
			return 30;
		}

		if (month == 2)
		{
			if ( (year%4 == 0) && (year%100 != 0) || (year%400 == 0) )
			{
				return 29;
			}
			return 28;
		}

		return 31;
	}
	
	public static void main(String[] args)
	{
		//System.out.println(getMaxDayOfMonth(2005,2));
	}

	/**
	 * ���month����0�õ�date<br>
	 * С��0���õ�dateʱ��ǰmonth���µ�ʱ�̶���<br>
	 * ����0���õ�dateʱ�̺�month���µ�ʱ�̶���
	 * @param date ʱ�����
	 * @param month ����
	 * @return ʱ�����
	 */
	public static AppliedDate getDateByMonth(AppliedDate date, int month)
	{
		if (month == 0)
		{
			return date;
		}

		int theyear  = date.getYear();
		int themonth = date.getMonth();
		int theday   = date.getDay();

		//����·�
		if (month > 0)
		{
			//�õ��ۼӵ�����
			themonth = themonth + month;
			//��λ�µ����
			theyear  = theyear + (themonth / 12);
			//��λ�µ��·�
			themonth = themonth % 12;
			//����·�Ϊ0����λ��12�����һ
			if (themonth == 0)
			{
				themonth = 12;
				theyear--;
			}
		}
		//�����·�
		else
		{
			//�õ��ۼ���·�
			themonth = themonth - Math.abs(month);
			//��������
			if (themonth < 1)
			{
				//���¶�λ�µ����
				theyear = theyear - ( Math.abs(themonth) / 12 + 1 );
				//���¶�λ�µ��·�
				themonth = 12 - (Math.abs(themonth) % 12);
			}
		}
		int maxday = getMaxDayOfMonth(theyear, themonth);
		if (theday > maxday)
		{
			theday = maxday;
		}

		AppliedDate tmpDate = new AppliedDate();
		tmpDate.set(theyear, themonth, theday, date.getHour(), date.getMinute(), date.getSecond());
		return tmpDate;
	}

	/**
	 * �õ�ָ��������ʱ�����Ӧ��ʱ�����
	 * @param year ��
	 * @param month ��
	 * @param day ��
	 * @param hour ʱ
	 * @param minute ��
	 * @param second ��
	 * @return ʱ�����
	 */
	public static AppliedDate getCurrentDate(int year, int month, int day, int hour, int minute, int second)
	{
		month = getCurMonth(month);
		day = getCurDay( year ,month ,day );
		AppliedDate tmpDate = new AppliedDate();
		tmpDate.set(year, month, day, hour, minute, second);
		return tmpDate;
	}

	/**
	 * �õ�ָ�������ն�Ӧ��ʱ�����ʱ����Ϊ��ǰʱ���ʱ����
	 * @param year ��
	 * @param month ��
	 * @param day ��
	 * @return ʱ�����
	 */
	public static AppliedDate getCurrentDate(int year, int month, int day)
	{
		month = getCurMonth(month);
		day = getCurDay( year ,month ,day );
		AppliedDate tmpDate = new AppliedDate();
		tmpDate.set(year, month, day);
		return tmpDate;
	}

	/**
	 * �õ�ָ��theDate�������쵱ǰʱ�̵�ʱ�����
	 * @param theDate ʱ�����
	 * @return ���쵱ǰʱ�̵�ʱ�����
	 */
	public static AppliedDate getYesterday(AppliedDate theDate)
	{
		int year   = theDate.getYear();
		int month  = theDate.getMonth();
		int day    = theDate.getDay();
		int hour   = theDate.getHour();
		int minute = theDate.getMinute();
		int second = theDate.getSecond();

		//���Ϊ1��1��
		if (month == 1 && day == 1)
		{
			year--;
			month = 12;
			day   = 31;
		}
		//���Ϊ1��
		else if (day == 1)
		{
			month--;
			day = getMaxDayOfMonth(year, month);
		}
		else
		{
			day--;
		}
		AppliedDate tmpDate = new AppliedDate();
		tmpDate.set(year, month, day, hour, minute, second);
		return tmpDate;
	}

	/**
	 * �õ������ַ�timeStr���ʱ���'YYYY��MM��DD��HHʱMM��SS��'��ʽ�ַ�
	 * @param timeStr ��Ҫת�����ַ�
	 * @return ���timeStr����Ϊ6����'HHʱMM��SS��'<br>
	 * Ϊ8����'YYYY��MM��DD��'<br>
	 * Ϊ14����'YYYY��MM��DD��HHʱMM��SS��'<br>
	 * ����ر���
	 */
	public static String getDateTimeWithName(String timeStr)
	{
		switch (timeStr.length())
		{
			case 6 :
				return timeStr.substring(0,2) + "ʱ" + timeStr.substring(2,4) + "��" + timeStr.substring(4,6) + "��";
			case 8 :
				return timeStr.substring(0,4) + "��" + timeStr.substring(4,6) + "��" + timeStr.substring(6,8) + "��";
			case 10 :
				return  getDateTimeWithName(timeStr.substring(0,8)) + " " + getDateTimeWithName(timeStr.substring(8,10) + "0000");
			case 12 :
				return  getDateTimeWithName(timeStr.substring(0,8)) + " " + getDateTimeWithName(timeStr.substring(8,12) + "00");
			case 14 :
				return getDateTimeWithName(timeStr.substring(0,8)) + getDateTimeWithName(timeStr.substring(8,14));
			default :
				return timeStr;
		}
	}

	/**
	 * �õ������ַ�timeStr���ʱ���'YYYY-MM-DD HH:MM:SS'��ʽ�ַ�
	 * @param timeStr ��Ҫת�����ַ�
	 * @return ���timeStr����Ϊ6����'HH:MM:SS'<br>
	 * Ϊ8����'YYYY-MM-DD'<br>
	 * Ϊ14����'YYYY-MM-DD HH:MM:SS'<br>
	 * ����ر���
	 */
	public static String getDateTime(String timeStr)
	{
		switch (timeStr.length())
		{
			case 6 :
				return timeStr.substring(0,2) + ":" + timeStr.substring(2,4) + ":" + timeStr.substring(4,6);
			case 8 :
				return timeStr.substring(0,4) + "-" + timeStr.substring(4,6) + "-" + timeStr.substring(6,8);
			case 10 :
				return  getDateTime(timeStr.substring(0,8)) + " " + getDateTime(timeStr.substring(8,10) + "0000");
			case 12 :
				return  getDateTime(timeStr.substring(0,8)) + " " + getDateTime(timeStr.substring(8,12) + "00");
			case 14 :
				return getDateTime(timeStr.substring(0,8)) + " " + getDateTime(timeStr.substring(8,14));
			default :
				return timeStr;
		}
	}

	/**
	 * �õ������ַ�timeStr���ʱ���'YYYYsepMMsepDD'��ʽ������'HHsepMMsepSS'��ʽ�ַ�
	 * @param timeStr ��Ҫת�����ַ�
	 * @param sep �ָ��
	 * @return ���timeStr����Ϊ6����'HHsepMMsepSS'��
	 * Ϊ8����'YYYYsepMMsepDD'������ر���
	 */
	public static String getDateTimeWithSep(String timeStr, String sep)
	{
		switch (timeStr.length())
		{
			case 6 :
				return timeStr.substring(0,2) + sep + timeStr.substring(2,4) + sep + timeStr.substring(4,6);
			case 8 :
				return timeStr.substring(0,4) + sep + timeStr.substring(4,6) + sep + timeStr.substring(6,8);
			default :
				return timeStr;
		}

	}

	/**
	 * �õ������ַ�timeStr���ʱ���'YYYYsepdateMMsepdateDD'��ʽ<br>
	 * ����'HHseptimeMMseptimeSS'��ʽ�ַ�<br>
	 * ����'YYYYsepdateMMsepdateDDsepHHseptimeMMseptimeSS'��ʽ�ַ�
	 * @param timeStr ��Ҫת�����ַ�
	 * @param datesep ���ڼ�ķָ��
	 * @param sep ���ں�ʱ���ķָ��
	 * @param timesep ʱ���ķָ��
	 * @return ���timeStr����Ϊ6����'HHseptimeMMseptimeSS'<br>
	 * Ϊ8����'YYYYsepdateMMsepdateDD'<br>
	 * Ϊ14����'YYYYsepdateMMsepdateDDsepHHseptimeMMseptimeSS'<br>
	 * ����ر���
	 */
	public static String getDateTimeWithSep(String timeStr, String datesep, String sep, String timesep)
	{
		switch (timeStr.length())
		{
			case 6 :
				return getDateTimeWithSep(timeStr, timesep);
			case 8 :
				return getDateTimeWithSep(timeStr, datesep);
			case 10 :
				return  getDateTimeWithSep(timeStr.substring(0,8), datesep) + sep + getDateTimeWithSep(timeStr.substring(8,10) + "0000", timesep);
			case 12 :
				return  getDateTimeWithSep(timeStr.substring(0,8), datesep) + sep + getDateTimeWithSep(timeStr.substring(8,12) + "00", timesep);
			case 14 :
				return getDateTimeWithSep(timeStr.substring(0,8), datesep) + sep + getDateTimeWithSep(timeStr.substring(8,14), timesep);
			default :
				return timeStr;
		}
	}
}
